<template>
  <div class="login">


    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">凌云后台管理系统</h3>
      <el-form-item prop="userName">
        <el-input v-model="loginForm.userName" type="text" auto-complete="off" placeholder="账号">
          <i slot="prefix" class="el-icon-user"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
        >
          <i slot="prefix" class="el-icon-lock"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="verificationCode" v-if="captchaOnOff">
        <el-input
            v-model="loginForm.verificationCode"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
        >
          <i slot="prefix" class="el-icon-circle-check"/>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" alt="验证码"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>

        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>


    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2021-2022 meishibiexuejava.cn All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>

import {getCodeImg} from "@/api/admin/login";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        // 账号
        userName: "2418870649@qq.com",
        // 密码
        password: "nacos",
        // 验证码
        verificationCode: "",
        keyCode: ""
      },
      // 遮罩
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      // 验证
      loginRules: {
        userName: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        verificationCode: [
          {required: true, trigger: "change", message: "请输入验证码"}
        ]
      },

    }


  },
  methods: {

    // 获取图形验证码
    getCode() {
      getCodeImg().then(res => {
        if (this.captchaOnOff) {
          this.codeUrl = res.image;
          this.loginForm.keyCode = res.keyCode;
        }
      });
    },

    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({path: "/admin"}).catch(() => {});
          }).catch(() => {
            this.loading = false;
            this.getCode();
          });
        }
      })
    }

  },

  // 监听
  watch: {
    $route: {
      handler: function (route) {
        console.log("当前路径")
        console.log(route)
      },
      immediate: true
    }
  },

  // 初始化
  created() {
    this.getCode()
  }

};
</script>

<style scoped>

.login {
  background-image: url("../assets/beijing.jpg");
  background-size: cover;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.login-form {
  border-radius: 6px;
  padding: 25px 25px 5px 25px;
  background: #fff;
  min-width: 260px;
  width: 368px;
  margin: 0 auto;
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form .el-input {
  height: 38px;
}

.login-form .el-input input {
  height: 38px;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
}

.login-code img {
  cursor: pointer;
  vertical-align: middle;
}

.login-code-img {
  height: 38px;
}

.el-login-footer {
  position: absolute;
  width: 100%;
  bottom: 0;
  padding: 0 16px;
  margin: 48px 0 24px;
  text-align: center;

  height: 40px;
  line-height: 40px;
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
}

</style>
